<!DOCTYPE html>
<html>
  <head>
    <meta charset='utf-8'>
    <meta content='width=device-width, initial-scale=1.0' name='viewport'>
    <meta content='' name='description'>
    <meta content='Nils Nordman' name='author'>
    <link href='/images/howl.png' rel='shortcut icon'>
    <title>Howl :: Spec - howl.dispatch</title>
    <link href="/stylesheets/bootstrap.min.css" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/syntax.css" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/howl.css" media="screen" rel="stylesheet" type="text/css" />
    <link href='//fonts.googleapis.com/css?family=Josefin+Slab' rel='stylesheet' type='text/css'>
    <link href='//fonts.googleapis.com/css?family=Open+Sans+Condensed:700' rel='stylesheet' type='text/css'>
  </head>
  <body class='doc doc_spec doc_spec_dispatch_spec'>
    <div class='container'>
      <div class='masthead'>
        <ul class='nav nav-pills'>
          <li>
            <a href='/'>
              <span class='glyphicon glyphicon-home'></span>
              Home
            </a>
          </li>
          <li>
            <a href='/doc/'>
              <span class='glyphicon glyphicon-book'></span>
              Documentation
            </a>
          </li>
          <li>
            <a href='/blog/'>
              <span class='glyphicon glyphicon-bullhorn'></span>
              Blog
            </a>
          </li>
          <li>
            <a href='/contact.html'>
              <span class='glyphicon glyphicon-inbox'></span>
              Contact
            </a>
          </li>
        </ul>
      </div>
      <ol class="breadcrumb"><li><a href="/">Home</a></li><li><a href='../'>Howl 0.3 Documentation</a></li><li>Spec</li><li>Spec - howl.dispatch</li></ol>
      <div class="spec-group spec-group-1">&#x000A;&#x000A;<h1 id="howl.dispatch">howl.dispatch</h1>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="launch">launch<span class="arg-list">(f, ...)</span></h2>&#x000A;&#x000A;<h4 id="invokes-&lt;f&gt;-in-a-coroutine-with-the-specified-arguments">invokes &lt;f&gt; in a coroutine with the specified arguments</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">spy</span><span class="p">.</span><span class="n">new</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">is_main</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">coroutine.running</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">is_main</span><span class="w">&#x000A;&#x000A;</span><span class="n">dispatch</span><span class="p">.</span><span class="n">launch</span><span class="w"> </span><span class="n">f</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="s1">'three'</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">spy</span><span class="p">(</span><span class="n">f</span><span class="p">).</span><span class="n">was_called_with</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="s1">'three'</span></code></pre>&#x000A;&#x000A;<div class="spec-group spec-group-3">&#x000A;&#x000A;<h3 id=""><span class="arg-list">(when &lt;f&gt; starts correctly)</span></h3>&#x000A;&#x000A;<h4 id="returns-true-and-the-coroutine-status">returns true and the coroutine status</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">status</span><span class="p">,</span><span class="w"> </span><span class="n">co_status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dispatch</span><span class="p">.</span><span class="n">launch</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="kc">nil</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">status</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="s1">'dead'</span><span class="p">,</span><span class="w"> </span><span class="n">co_status</span></code></pre>&#x000A;&#x000A;</div>&#x000A;<div class="spec-group spec-group-3">&#x000A;&#x000A;<h3 id=""><span class="arg-list">(when &lt;f&gt; errors upon start)</span></h3>&#x000A;&#x000A;<h4 id="returns-false-and-the-error-message">returns false and the error message</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">status</span><span class="p">,</span><span class="w"> </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dispatch</span><span class="p">.</span><span class="n">launch</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="nb">error</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">status</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="s1">'foo'</span><span class="p">,</span><span class="w"> </span><span class="n">err</span></code></pre>&#x000A;&#x000A;</div>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="wait">wait<span class="arg-list">()</span></h2>&#x000A;&#x000A;<h4 id="yields-until-resumed-using-resume()-on-the-parked-handle">yields until resumed using resume() on the parked handle</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">handle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dispatch</span><span class="p">.</span><span class="n">park</span><span class="w"> </span><span class="s1">'test'</span><span class="w">&#x000A;</span><span class="n">done</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="w">&#x000A;&#x000A;</span><span class="n">dispatch</span><span class="p">.</span><span class="n">launch</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">dispatch</span><span class="p">.</span><span class="n">wait</span><span class="w"> </span><span class="n">handle</span><span class="w">&#x000A;  </span><span class="n">done</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="w">&#x000A;&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">done</span><span class="w">&#x000A;</span><span class="n">dispatch</span><span class="p">.</span><span class="n">resume</span><span class="w"> </span><span class="n">handle</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">done</span></code></pre>&#x000A;&#x000A;&#x000A;<h4 id="returns-any-parameters-passed-to-resume()">returns any parameters passed to resume()</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">handle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dispatch</span><span class="p">.</span><span class="n">park</span><span class="w"> </span><span class="s1">'test'</span><span class="w">&#x000A;</span><span class="kd">local</span><span class="w"> </span><span class="n">res</span><span class="w">&#x000A;&#x000A;</span><span class="n">dispatch</span><span class="p">.</span><span class="n">launch</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">dispatch</span><span class="p">.</span><span class="n">wait</span><span class="w"> </span><span class="n">handle</span><span class="w"> </span><span class="p">}</span><span class="w">&#x000A;&#x000A;</span><span class="n">dispatch</span><span class="p">.</span><span class="n">resume</span><span class="w"> </span><span class="n">handle</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="s1">'three'</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="s1">'three'</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="n">res</span></code></pre>&#x000A;&#x000A;&#x000A;<h4 id="raises-an-error-when-resumed-with-resume_with_error()">raises an error when resumed with resume_with_error()</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">handle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dispatch</span><span class="p">.</span><span class="n">park</span><span class="w"> </span><span class="s1">'test'</span><span class="w">&#x000A;</span><span class="kd">local</span><span class="w"> </span><span class="n">err</span><span class="w">&#x000A;&#x000A;</span><span class="n">dispatch</span><span class="p">.</span><span class="n">launch</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">status</span><span class="p">,</span><span class="w"> </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">pcall</span><span class="w"> </span><span class="n">dispatch</span><span class="p">.</span><span class="n">wait</span><span class="p">,</span><span class="w"> </span><span class="n">handle</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">status</span><span class="w">&#x000A;&#x000A;</span><span class="n">dispatch</span><span class="p">.</span><span class="n">resume_with_error</span><span class="w"> </span><span class="n">handle</span><span class="p">,</span><span class="w"> </span><span class="s1">'blargh!'</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">includes</span><span class="w"> </span><span class="n">err</span><span class="p">,</span><span class="w"> </span><span class="s1">'blargh!'</span></code></pre>&#x000A;&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="resume">resume<span class="arg-list">()</span></h2>&#x000A;&#x000A;<h4 id="propagates-any-error-occurring-during-resuming">propagates any error occurring during resuming</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">handle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dispatch</span><span class="p">.</span><span class="n">park</span><span class="w"> </span><span class="s1">'test'</span><span class="w">&#x000A;&#x000A;</span><span class="n">dispatch</span><span class="p">.</span><span class="n">launch</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">dispatch</span><span class="p">.</span><span class="n">wait</span><span class="w"> </span><span class="n">handle</span><span class="w">&#x000A;  </span><span class="nb">error</span><span class="w"> </span><span class="s1">'boom'</span><span class="w">&#x000A;&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">raises</span><span class="w"> </span><span class="s1">'boom'</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">dispatch</span><span class="p">.</span><span class="n">resume</span><span class="w"> </span><span class="n">handle</span></code></pre>&#x000A;&#x000A;<div class="spec-group spec-group-3">&#x000A;&#x000A;<h3 id=""><span class="arg-list">(when nothing is yet waiting on the parking)</span></h3>&#x000A;&#x000A;<h4 id="------it-'blocks-until-released-by-a-wait',-(done)--&gt;&#x000A;">      it 'blocks until released by a wait', (done) -&gt;&#x000A;</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">howl_async</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">handle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dispatch</span><span class="p">.</span><span class="n">park</span><span class="w"> </span><span class="s1">'out-of-order'</span><span class="w">&#x000A;  </span><span class="n">launched</span><span class="p">,</span><span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dispatch</span><span class="p">.</span><span class="n">launch</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">dispatch</span><span class="p">.</span><span class="n">resume</span><span class="w"> </span><span class="n">handle</span><span class="p">,</span><span class="w"> </span><span class="s1">'resume-now!'</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">launched</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="s2">"suspended"</span><span class="p">,</span><span class="w"> </span><span class="n">status</span><span class="w">&#x000A;&#x000A;  </span><span class="n">launched</span><span class="p">,</span><span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dispatch</span><span class="p">.</span><span class="n">launch</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;    </span><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="s1">'resume-now!'</span><span class="p">,</span><span class="w"> </span><span class="n">dispatch</span><span class="p">.</span><span class="n">wait</span><span class="w"> </span><span class="n">handle</span><span class="w">&#x000A;    </span><span class="n">done</span><span class="o">!</span><span class="w">&#x000A;&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">launched</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="s2">"dead"</span><span class="p">,</span><span class="w"> </span><span class="n">status</span></code></pre>&#x000A;&#x000A;</div>&#x000A;</div>&#x000A;</div>
      <div class='footer text-muted'>
        <a href='/'>
          <img width="50" height="50" class="footer-logo" src="/images/howl.png" />
        </a>
        <div class='footer-follow'>
          <p>
            <a class='twitter-follow-button' data-lang='en' data-show-count='false' href='https://twitter.com/howleditor' rel='me'>
              Follow @howleditor
            </a>
          </p>
          <p>
            <a class='twitter-share-button' data-count='none' data-hashtags='howleditor' data-lang='en' data-text='The Howl Editor, a general purpose, light-weight customizable editor.' data-url='http://howl.io' href='https://twitter.com/share'>
              Tweet
            </a>
          </p>
        </div>
        <div class='footer-blurb'>
          <div>The Howl editor.</div>
          <div>
            Copyright 2012-2015
            <a class='alert-link' href='https://github.com/nilnor/howl/contributors'>
              The Howl Developers.
            </a>
          </div>
        </div>
      </div>
    </div>
    <script>
      <!-- / GA -->
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-45283282-1', 'howl.io');
      ga('send', 'pageview');
      <!-- / Twitter -->
      !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];
      if(!d.getElementById(id)){js=d.createElement(s);js.id=id;
      js.src="//platform.twitter.com/widgets.js";
      fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
    </script>
  </body>
</html>
